﻿using System.Windows;
using System.Windows.Controls;

namespace WpfApp1;

public partial class ComboBoxWindow : Window
{
    public ComboBoxWindow()
    {
        InitializeComponent();
    }

    private void Selector_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        //sender是object类型，不能直接访问SelectedItem属性，必须先转换为Listbox类型
        ListBox? listBox = sender as ListBox;
        //SelectedItem 属性被定义为 object 类型，这样它可以容纳任何类型的对象。需要先将 SelectedItem 转换为 ListBoxItem，然后再访问其 Content 属性
        ListBoxItem? selectedItem = listBox.SelectedItem as ListBoxItem;
        MessageBox.Show(selectedItem.Content.ToString());

    }

    private void Selector_OnSelectionChanged2(object sender, SelectionChangedEventArgs e)
    {
        ComboBox? comboBox = sender as ComboBox;
        ComboBoxItem selectedItem = comboBox.SelectedItem as ComboBoxItem;
        MessageBox.Show(selectedItem.Content.ToString());
    }
}